OBO=http://purl.obolibrary.org/obo

# ========================================
# PRODUCTION
# ========================================

build: ro.owl ro.obo ro-el.owl all_subsets

# fairly dump release process whilst waiting for Oort - check than versionIRI has been entered manually.
ro.owl: ro-edit.owl core.owl temporal-intervals.owl validate-using-oort
	owltools $<  --set-ontology-id -v $(OBO)/ro/releases/`date +%Y-%m-%d`/ro.owl $(OBO)/ro.owl -o file://`pwd`/$@
#	grep versionIRI $< && (echo "check the version:" && echo versionIRI: `grep versionIRI $<` && cp $< $@) || echo 'You must set the versionIRI!!'

%.obo: %.owl
	owltools --use-catalog $< --merge-import-closure -o -f obo $@.tmp && grep -v ^synonym $@.tmp > $@
#	obolib-owl2obo -o $@.tmp $< && grep -v ^import: $@.tmp > $@

%-sh.obo: %.owl
	owltools --use-catalog $< --merge-import-closure --add-obo-shorthand-to-properties -o -f obo $@.tmp && grep -v ^owl-axioms $@.tmp >  $@

# soon this will be incorporated into Oort
ro-el.owl: ro.owl
	makeElWithoutReasoning.sh -i `pwd`/$< -o `pwd`/$@

# note we don't do anything with the generated ontology, this is just for checking purposes.
validate-using-oort: ro-edit.owl
	ontology-release-runner  --catalog-xml catalog-v001.xml --outdir build --simple --allow-overwrite --reasoner hermit ro-edit.owl
	touch $@



# ========================================
# DIFFS
# ========================================
ro-lastbuild.owl: ro-edit.owl
	wget $(OBO)/ro.owl -O $@

ro-diff.md: ro.owl ro-lastbuild.owl 
	owljs-diff -o $@ ro-lastbuild.owl $<
ro-diff.html: ro-diff.md
	pandoc $< -o $@

# ========================================
# IMPORTS
# ========================================
seed.owl: ro-edit.owl
	owltools --use-catalog $< --merge-imports-closure --remove-imports-declarations -o $@

# TODO - have to manually remove OPs for now...
#pato_mirror.owl: ro-edit.owl
#	owltools $(OBO)/pato.owl --extract-mingraph --make-subset-by-properties // --remove-dangling-annotations --set-ontology-id $(OBO)/pato.owl -o $@
#pato_import.owl: seed.owl pato_mirror.owl
#	owltools --use-catalog $< pato_mirror.owl --add-imports-from-supports --extract-module -c -s $(OBO)/pato.owl --set-ontology-id $(OBO)/ro/$@ -o $@
##	owltools --use-catalog --map-ontology-iri $(OBO)/ro/pato_import.owl pato_mirror.owl ro-edit.owl --extract-module -s $(OBO)/pato.owl --set-ontology-id $(OBO)/ro/$@ -o $@


# ========================================
# MIREOT
# ========================================

bfo2-classes.owl:
	owltools $(OBO)/bfo.owl --make-subset-by-properties // --set-ontology-id $(OBO)/ro/bfo2-classes.owl -o file://`pwd`/bfo2-classes.owl

# ========================================
# SUBSETS
# ========================================
SUBSETS = interaction sequence time-interval mereotopology causal developmental eco hom
all_subsets: all_subsets_owl all_subsets_obo
all_subsets_owl: $(patsubst %,subsets/ro-%.owl,$(SUBSETS))
all_subsets_obo: $(patsubst %,subsets/ro-%.obo,$(SUBSETS))

# generated by oort:
subsets/ro-eco.owl: build/subsets/ro-eco.owl
	owltools --use-catalog $< --remove-dangling -o $@

# the following subsets are generated purely by hierarchy:
subsets/ro-interaction.owl: ro.owl
	owltools --use-catalog $< --extract-properties -s --list 'interacts with' // --set-ontology-id $(OBO)/ro/$@ -o $@
subsets/ro-sequence.owl: ro.owl
	owltools --use-catalog $< --extract-properties -s --list 'sequentially related to' // --set-ontology-id $(OBO)/ro/$@ -o $@
subsets/ro-time-interval.owl: ro.owl
	owltools --use-catalog $< --extract-properties -s --list 'temporally related to' // --set-ontology-id $(OBO)/ro/$@ -o $@
subsets/ro-mereotopology.owl: ro.owl
	owltools --use-catalog $< --extract-properties -s --list 'mereotopologically related to' // --set-ontology-id $(OBO)/ro/$@ -o $@
subsets/ro-causal.owl: ro.owl
	owltools --use-catalog $< --extract-properties -s --list 'causally related to' // --set-ontology-id $(OBO)/ro/$@ -o $@
subsets/ro-developmental.owl: ro.owl
	owltools --use-catalog $< --extract-properties -s --list 'developmentally related to' // --set-ontology-id $(OBO)/ro/$@ -o $@
#subsets/ro-eco.owl: ro.owl
#	owltools --use-catalog $< --extract-properties -s --list 'ecologically related to' // --set-ontology-id $(OBO)/ro/$@ -o $@
subsets/ro-hom.owl: ro.owl
	owltools --use-catalog $< --extract-properties -s --list 'in similarity relationship with' // --set-ontology-id $(OBO)/ro/$@ -o $@

# ========================================
# EXPERIMENTAL
# ========================================

# Oort requires more testing before it can be used for a relations ontology.
#  for now, just go with a simple release process (see above)
build-using-oort: ro-edit.owl
	ontology-release-runner --catalog-xml catalog-v001.xml --outdir build --simple --allow-overwrite --reasoner hermit ro-edit.owl
	touch $@

hom-fixed.obo: hom.obo
	obo-grep.pl --neg -r is_obsolete: $< > $@.tmp && mv $@.tmp $@

rohom.obo: hom-fixed.obo
	../tools/hom2rhom.pl $< > $@.tmp && mv $@.tmp $@
rohom.owl: rohom.obo
	owltools $< -o $@.tmp && grep -v 'oboInOwl:id' $@.tmp > $@.tmp2 && $(HOME)/repos/uberon/util/expand-dbxref-literals.pl $@.tmp2 > $@

## ODPs

odp/%.owl:
	wget http://www.ontologydesignpatterns.org/cp/owl/$*.owl -O $@
